﻿using System;
using hundsun.t2sdk;

namespace QuantBox.XApi
{
    internal static class UfxExtension
    {
        public static UfxErrorInfo ReadErrorInfo(this CT2UnPacker result)
        {
            result.Next();
            var msg = $"MsgCallback,{result.GetErrMsg()},{result.GetMsgDetail()}";
            return new UfxErrorInfo(result.GetErrCode(), msg);
        }

        public static UfxErrorInfo ReadErrorInfo(this CT2UnPacker result, int functionId)
        {
            result.Next();
            var code = result.GetErrCode();
            if (code == 0) {
                return UfxErrorInfo.Ok;
            }

            var msg = $"{UfxNames.GetFunctionName(functionId)}({functionId}),{result.GetErrMsg()},{result.GetMsgDetail()}";
            return new UfxErrorInfo(code, msg);
        }

        public static ErrorField ToErrorField(this UfxErrorInfo info, string source = "")
        {
            return new ErrorField(info.ErrCode, 0, info.ErrMsg, source);
        }

        public static int ToIntTime(this DateTime dateTime)
        {
            return dateTime.Hour * 10000 + dateTime.Minute * 100 + dateTime.Second;
        }

        public static int ToIntDate(this DateTime dateTime)
        {
            return dateTime.Year * 10000 + dateTime.Month * 100 + dateTime.Day;
        }
    }
}